﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using celinda.Repository;
using celinda.Class;
using celinda.Formulaire.Tiers;

namespace celinda.Formulaire.Parametre
{
    public partial class UtilisateurForm : BaseForm
    {
        public UtilisateurForm()
        {
            InitializeComponent();
        }

        protected override void Ajout()
        { }

        protected override void Supprime()
        { }

        protected override void Dupplique()
        { }

        protected override void Valide()
        { }

        protected override void ChangeEtat()
        { }

        private void ChargeSociete()
        {
            SocieteRepositoryNH repS = new SocieteRepositoryNH();
            IList<Societe> ls = repS.GetAllActif();

            kCbSociete.DataSource = ls;

        }

        private void UtilisateurForm_Load(object sender, EventArgs e)
        {
            ChargeSociete();
            ChargeFormInit();

        }

        private void kTvUtilisateur_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node.Name != string.Empty)
            {
                ChargeForm(e.Node.Name);
            }
        }

        private void ChargeFormInit()
        {
            kTvUtilisateur.Nodes.Clear();

            Int32 count = 0;
            tssMode.Text = Const.ModeCreation.ToString();
            UtilisateurRepositoryNH repU = new UtilisateurRepositoryNH();
            SocieteRepositoryNH repS = new SocieteRepositoryNH();

            IList<Utilisateur> lu = repU.GetUtilisateurSansSociete();
            IList<Societe> ls = repS.GetAll();

            TreeNode trTemp = new TreeNode("Sans Société", 0, 0);
            foreach (Utilisateur u in lu)
            {
                TreeNode trTemp2 = new TreeNode(u.Login, 1, 1);
                trTemp2.Name = u.Id.ToString();
                trTemp.Nodes.Add(trTemp2);
                count++;
            }
            kTvUtilisateur.Nodes.Add(trTemp);

            foreach (Societe s in ls)
            {
                trTemp = new TreeNode(s.Nom, 0, 0);
                lu = repU.GetUtilisateurAvecSociete(s);
                foreach (Utilisateur u in lu)
                {
                    TreeNode trTemp2 = new TreeNode(u.Login, 1, 1);
                    trTemp2.Name = u.Id.ToString();
                    trTemp.Nodes.Add(trTemp2);
                    count++;
                }
                kTvUtilisateur.Nodes.Add(trTemp);
            }
            tssNbLigneGrid.Text = count.ToString();
        }

        private void ChargeForm(string id)
        {
            UtilisateurRepositoryNH repU = new UtilisateurRepositoryNH();
            Utilisateur u = repU.GetByID(Int32.Parse(id));
            tssMode.Text = Const.ModeModification.ToString();

            kTxtLogin.Text = u.Login;
            kTxtMdp.Text = u.MotDePasse;
            kTxtMdp2.Text = u.MotDePasse;
            if (u.Societe != null)
                kCbSociete.ValueMember = Convert.ToString(u.Societe.Id);
            else
                kCbSociete.ValueMember = null;

        }

        private void btnGestSociete_Click(object sender, EventArgs e)
        {
            SocieteForm sF = new SocieteForm();
            sF.MdiParent = this.MdiParent;
            sF.Show();
        }

        private void kBtnRefreshSociete_Click(object sender, EventArgs e)
        {
            ChargeFormInit();
            ChargeSociete();
        }
    }
}
